/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package dao;

import dao.exceptions.NonexistentEntityException;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import modelo.Diploma;
import modelo.TelefoneDiploma;

/**
 *
 * @author Dirac
 */
public class TelefoneDiplomaJpaController {

    public TelefoneDiplomaJpaController() {
        emf = Persistence.createEntityManagerFactory("ArquivoPU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(TelefoneDiploma telefoneDiploma) {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            em.persist(telefoneDiploma.getTelefone());
            em.persist(telefoneDiploma);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(TelefoneDiploma telefoneDiploma) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            TelefoneDiploma persistentTelefoneDiploma = em.find(TelefoneDiploma.class, telefoneDiploma.getId());
            Diploma diplomaOld = persistentTelefoneDiploma.getDiploma();
            Diploma diplomaNew = telefoneDiploma.getDiploma();
            if (diplomaNew != null) {
                diplomaNew = em.getReference(diplomaNew.getClass(), diplomaNew.getId());
                telefoneDiploma.setDiploma(diplomaNew);
            }
            telefoneDiploma = em.merge(telefoneDiploma);
            if (diplomaOld != null && !diplomaOld.equals(diplomaNew)) {
                diplomaOld.getTelefoneDiplomas().remove(telefoneDiploma);
                diplomaOld = em.merge(diplomaOld);
            }
            if (diplomaNew != null && !diplomaNew.equals(diplomaOld)) {
                diplomaNew.getTelefoneDiplomas().add(telefoneDiploma);
                diplomaNew = em.merge(diplomaNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = telefoneDiploma.getId();
                if (findTelefoneDiploma(id) == null) {
                    throw new NonexistentEntityException("The telefoneDiploma with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            TelefoneDiploma telefoneDiploma;
            try {
                telefoneDiploma = em.getReference(TelefoneDiploma.class, id);
                telefoneDiploma.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The telefoneDiploma with id " + id + " no longer exists.", enfe);
            }
            
            em.remove(telefoneDiploma);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<TelefoneDiploma> findTelefoneDiplomaEntities() {
        return findTelefoneDiplomaEntities(true, -1, -1);
    }

    public List<TelefoneDiploma> findTelefoneDiplomaEntities(int maxResults, int firstResult) {
        return findTelefoneDiplomaEntities(false, maxResults, firstResult);
    }

    private List<TelefoneDiploma> findTelefoneDiplomaEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select object(o) from TelefoneDiploma as o");
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public TelefoneDiploma findTelefoneDiploma(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(TelefoneDiploma.class, id);
        } finally {
            em.close();
        }
    }

    public int getTelefoneDiplomaCount() {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select count(o) from TelefoneDiploma as o");
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

    public List<TelefoneDiploma> findToDiploma(Diploma diploma) {
         EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("SELECT t FROM TelefoneDiploma t WHERE t.diploma.id = :diploma");
            q.setParameter("diploma", diploma.getId());
            
            return q.getResultList();
        } finally {
            em.close();
        }
    }

}
